<template>
	<view>
		<Navbar :isReturn="true"></Navbar>
		<view class="u-m-20 u-m-b-25">
			<u-swiper :list="bannerList" :height="400"></u-swiper>
		</view>
		<!-- <view class="u-m-20 u-card-box u-flex">
			<view class="u-flex-1 u-m-r-20">
				<view class="u-color-red">礼包编号：{{ giftData.identifier || '' }}</view>
				<view class="u-font-34 u-font-bold u-m-t-10">{{giftData.title}}</view>
			</view>
			<view class="price_box">
				<view class="u-color-red">
					<span>¥</span>
					<span class="u-font-40 u-m-l-10 u-font-bold">{{ giftData.draw_price }}</span>
				</view>
				<view class="u-m-t-5 u-font-26 u-color-gray">
					<span>原价</span>
					<span class="u-m-l-10 u-font-line">¥{{ giftData.price }}</span>
				</view>
			</view>
		</view> -->
		<view class="u-m-20 u-m-b-25 u-card-box">
			<view class="u-font-34 u-font-bold">{{giftData.title}}</view>
			<view class="u-flex u-row-between u-m-t-20">
				<view class="u-flex">
					<u-icon name="clock-fill" color="#888B9C" size="34"></u-icon>
					<view class="u-font-26 u-m-l-10 u-color-gray">{{giftData.create_time}}</view>
				</view>
				<view class="u-flex">
					<u-icon name="eye-fill" color="#888B9C" size="34"></u-icon>
					<view class="u-font-26 u-m-l-10 u-color-gray">浏览量：{{giftData.view_number}}</view>
				</view>
			</view>
		</view>
		<view class="u-m-b-25 u-m-l-20 u-m-r-20 u-card-box" v-if="userData.id">
			<view class="u-flex">
				<view>
					<u-image width="140rpx" height="140rpx" border-radius="20"
						:src="userData.head_img"></u-image>
					<view class="u-text-center u-font-24 u-m-t-15 number_box">
						<view class="u-font-bold u-font-26">{{userData.number}}</view>
						<view class="u-font-23 u-m-l-5">能量值</view>
					</view>
				</view>

				<view class="u-m-l-20 u-flex-1 u-flex-col u-row-between u-ellipsis">
					<view class="u-flex u-row-between">
						<view class="u-flex">
							<view class="u-m-r-10 u-font-title">{{userData.name}}</view>
							<!-- <view class="u-m-r-10" v-if="![0,'0'].includes(userData.sex)">
								<u-icon size="32" :color="userData.sex == 1 ? '#4399FF' : '#FF69B1'"
									:name="userData.sex == 1 ? 'nanxing' : 'nvxing'" custom-prefix="custom-icon" />
							</view> -->
							<view class="u-m-r-10" v-if="userData.is_authentication == 2">
								<u-icon size="32" color="#FB6316" name="renzheng" custom-prefix="custom-icon" />
							</view>
							<view class="u-m-r-10" v-if="userData.is_company_user == 1">
								<u-icon size="32" color="#F8494D" name="qiye1" custom-prefix="custom-icon" />
							</view>
							<view class="u-tag-box" :class="userData.president_class"
								v-if="[5,6,7,8].includes(userData.president_status) && userData.president_class">
								{{ getPresidentName(userData.partner_status, userData.president_status) }}
							</view>
						</view>
						<view class="edit_btn" v-if="isPromoter" @click="onEditUserInfo">修改</view>
					</view>
					<view class="u-m-t-15 u-color-gray u-ellipsis">{{userData.address || ''}}</view>
					<view class="u-m-t-20 u-flex u-row-around u-width-100">
						<view class="u-text-center">
							<view class="u-color-gray u-font-24">举办酒局</view>
							<view class="u-font-bold u-font-34 u-m-t-10">{{userData.data_num.space || 0}}场</view>
						</view>
						<view class="line"></view>
						<view class="u-text-center">
							<view class="u-color-gray u-font-24">参与酒局</view>
							<view class="u-font-bold u-font-34 u-m-t-10">{{userData.data_num.join_num || 0}}场
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="u-m-l-20 u-m-r-20 u-card-box">
			<view class="u-m-t-20">
				<u-parse :html="giftData.content" :tag-style="style"></u-parse>
			</view>
		</view>
		<view class="u-m-l-20 u-m-r-20 u-m-t-25" v-if="giftData.pdf_name[0]">
			<view class="u-font-34 u-font-bold u-m-l-10">附件</view>
			<view class="sign_box" @click="onOpenDocument">
				<view class="agree_name u-font-bold">{{giftData.pdf_name[0]}}</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
		<view class="u-m-l-20 u-m-r-20 u-m-t-25" v-if="teaList[0]">
			<view class="u-font-34 u-font-bold u-m-l-10">相关酒局</view>
			<view class="u-m-t-25">
				<TeaList :list="teaList" @goAdvUrl="goAdvUrl" @goTeaDetail="goTeaDetail"
					@goSearch="goSearch"></TeaList>
			</view>
		</view>
		<view style="height: 200rpx;"></view>
		<Bottom :isTwice="userData.id ? false : true" :isThird="userData.id ? true : false" :rightText="userData.id ? '联系' : '分享'" :centerText="'分享'" @confirm="onOpenShare" @center="onOpenShare" @right="onCallDirector"></Bottom>
		
		<!-- 分享 -->
		<Share :isShowShare="shareShow" @closeShare="shareShow = false"></Share>
	</view>
</template>

<script>
	import {
		userTogetherInfo
	} from '@/network/api.js'
	import { teaList } from '@/network/tea.js'
	import {
		formatContent,
		PresidentLevel,
    TeaStatus,
		presidentClass,
		pointTransform,
		getDayOfWeek,
		call
	} from '@/utils/index.js'
	import Navbar from '@/components/Navbar.vue'
	import Bottom from '@/components/Bottom.vue'
	import TeaList from '@/components/TeaList.vue'
	import Share from '@/components/Share.vue'
	import share from '@/mixins/share.js'
	export default {
		mixins: [share],
		components: {
			Navbar,
			Bottom,
			TeaList,
			Share
		},
		data() {
			return {
				giftData: {},
				userData: {},
				bannerList: [],
				style: {
					video: 'width: 100% !important'
				},
				teaList: [],
				shareShow: false
			}
		},
		onLoad(options) {
			this.onGetGiftData(options.id)
		},
		methods: {
			async onGetGiftData(id) {
				const res = await userTogetherInfo({
					together_id: id
				})
				res.data.images.map(item => {
					const params = {
						image: item
					}
					this.bannerList.push(params)
				})
				res.data.price = parseInt(res.data.price)
				res.data.draw_price = parseInt(res.data.draw_price)
				res.data.content = formatContent(res.data.content)
				this.giftData = res.data
				if(res.data.user){
					res.data.user.president_status = parseInt(res.data.user.president_status)
					res.data.user.president_class = presidentClass(res.data.user.president_status)
					this.userData = res.data.user
				}
				this.mixinsShare.page =
					`/pages/giftDetail/giftDetail?id=${res.data.id}&inviterId=${uni.getStorageSync("user_id")}`
				this.mixinsShare.title = `${res.data.title}`
				this.mixinsShare.image = res.data.images[0] ||
					'https://medv-new.oss-cn-shanghai.aliyuncs.com/images/common/20240624979559784584.png'
				this.onGetTeaList()
			},
			getPresidentName(status1, status2) {
				// return PresidentLevel(status)
        return TeaStatus(status1, status2)  
			},
			async onGetTeaList(){
				const params = {
					tea_status: 1,
					label: this.giftData.identifier,
					page: 1,
					limit: 10
				}
				const res = await teaList(params)
				if (res.data.list.length > 0) {
					res.data.list.map(item => {
						switch (item.pay_type) {
							case 1:
								item.pay_name = '免费'
								break;
							case 2:
								item.pay_name = `${parseInt(item.price)}元`
								break;
							case 3:
								item.pay_name = `${parseInt(item.price)}积分`
								break;
						}
						if (item.label) {
							item.label = item.label.split("#").filter(item => item !==
								null && item !== undefined && item)
						}
						item.user.number = pointTransform(item.user.number)
						item.user.president_class = presidentClass(item.user
							.president_status)
						let newTime = item.starting_time.slice(0, 0) + item.starting_time
							.slice(11)
						item.starting_time = getDayOfWeek(item.starting_time.slice(0,
							10)) + newTime
						this.teaList.push(item)
					})
				}
			},
			onOpenDocument(){
				const _this = this
				wx.downloadFile({
					url: _this.giftData.pdf_file[0],
					header: {
						"Content-Type": "application/pdf;charset=UTF-8"
					},
					success: function(res) {
						if (res.statusCode == 200) {
							const filePath = res.tempFilePath;
							uni.showLoading({
								title: '正在打开',
								mask: true
							})
							wx.openDocument({
								filePath: filePath,
								fileType: 'pdf',
								showMenu: true,
								success: function(res) {
									uni.hideLoading()
								},
								fail: function(err) {
									uni.hideLoading()
								}
							});
						}
					},
					fail: function(err) {
						uni.hideLoading()
					}
				});
			},
			goTeaDetail(val) {
				uni.navigateTo({
					url: `/teaBureau/teaDetail/teaDetail?id=${val.id}`
				})
			},
			goThanksList(){
				uni.navigateTo({
					url: "/teaBureau/teaThanksList/teaThanksList?id=" + this.userData.id
				})
			},
			onCallDirector(){
				call(this.userData.mobile)
			},
			onOpenShare(){
				this.shareShow = true
			}
		}
	}
</script>

<style scoped>
	.price_box {
		background: #FFF0E9;
		padding: 20rpx;
		border-radius: 20rpx;
	}
	
	.sign_box{
		display: flex;
		justify-content: space-between;
		padding: 25rpx 20rpx;
		border-radius: 20rpx;
		font-size: 30rpx;
		border: 1rpx solid #ccc;
		margin-top: 20rpx;
		background: #fff;
	}
</style>